package com.example.lin.day9_26;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.lin.day9_26.pojo.ResultData;
import com.example.lin.day9_26.pojo.User;
import com.example.lin.day9_26.utils.ApiRequestService;
import com.example.lin.day9_26.utils.RetrofitClient;
import com.facebook.drawee.view.DraweeView;

import java.io.File;
import java.util.ArrayList;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        test1();
    }

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.3.100:8080/home/")
            //使用 RxJava 的响应式编程方式来处理 Retrofit的请求
//            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            //指定转换流 Gson
            .addConverterFactory(GsonConverterFactory.create())
            .build();



    public static final MediaType TYPE_FILE = MediaType.parse("application/octet-stream");

    private void test3() {

        File icon = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "timg.jpg");
        RetrofitClient.getClient().uploadIcon("fk", icon, new Observer<ResultData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ResultData resultData) {
                Toast.makeText(MainActivity.this, resultData.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable e) {
                Toast.makeText(MainActivity.this, "上传失败!" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete() {

            }
        });

//        ApiRequestService iUserRequest = retrofit.create(ApiRequestService.class);//动态代理
//
//        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "timg.jpg");
//        final RequestBody requestBody = RequestBody.create(TYPE_FILE, file);
//
//
//        MultipartBody.Part formData1 = MultipartBody.Part.createFormData("f1", "aaa" + file.getName(), requestBody);
//
//        MultipartBody.Part formData2 = MultipartBody.Part.createFormData("f2", "bbb" + file.getName(), requestBody);
//
//        ArrayList<MultipartBody.Part> parts = new ArrayList<>();
//        parts.add(formData1);
//        parts.add(formData2);
//
//
//        Call<ResultData> dataCall = iUserRequest.uploadIcon("测试,跟随图片一起提交的键值对", parts);
//
//
//        dataCall.enqueue(new Callback<ResultData>() {
//            @Override
//            public void onResponse(Call<ResultData> call, Response<ResultData> response) {
//                Toast.makeText(MainActivity.this, response.body().toString(), Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onFailure(Call<ResultData> call, Throwable t) {
//
//            }
//        });
    }


    private void test2() {

        RetrofitClient.getClient().getUser("无所谓", new Observer<ResultData<User>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ResultData<User> userResultData) {
                Toast.makeText(MainActivity.this, userResultData.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

//        ApiRequestService iUserRequest = retrofit.create(ApiRequestService.class);//动态代理
//        Observable<ResultData<User>> getUser = iUserRequest.getUser("随便");
//
//
//        Disposable subscribe = getUser
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<ResultData<User>>() {
//                    @Override
//                    public void accept(ResultData<User> userResultData) throws Exception {
//                        Toast.makeText(MainActivity.this, userResultData.toString(), Toast.LENGTH_SHORT).show();
//                    }
//                });

    }

    /**
     * Retrofit 实现POST请求 , 注册操作
     */
    private void test1() {

        RetrofitClient.getClient().register("a", "b", new Observer<ResultData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ResultData resultData) {
                Toast.makeText(MainActivity.this, resultData.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable e) {
                Toast.makeText(MainActivity.this, "请求失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onComplete() {

            }
        });

//
//        Disposable disposable = iUserRequest.register("abc1231111", "123456111")
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<ResultData>() {
//                    @Override
//                    public void accept(ResultData resultData) throws Exception {
//                        Toast.makeText(MainActivity.this, resultData.toString(), Toast.LENGTH_SHORT).show();
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//
//                        Toast.makeText(MainActivity.this, "请求失败" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
//                    }
//                });


//        disposable.dispose();//取消订阅(内部会自动断开OKHttp的连接)

    }
}
